using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace GoldDigger.Common
{
    public static class CollisionHelper
    {
        public static bool CollideWith(this Rectangle rect, Vector2 start, Vector2 end, int accuracy)
        {
            if (accuracy < 0 || accuracy > 10)
                throw new ArgumentOutOfRangeException("accuracy", "Accuracy value range is [1-10]");

            var distance = end - start;
            const int minpoint = 3;
            var segmentCount = accuracy*minpoint;
            var segmentLength = distance.Length()/segmentCount;
            
            // Remove distance information
            distance.Normalize();
            for (int i = 0; i < segmentCount; i++)
            {
                if (rect.Contains((int)start.X, (int)start.Y)) return true;
                start += distance*segmentLength;
            }
            return false;
        }
    }
}
